/*            Copyright(c) 2006-2008 Dave Sexton           *
 * http://www.codeplex.com/DocProject                      *
 * http://www.codeplex.com/DocProject/Project/License.aspx *
 ***********************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Build.Utilities;

namespace DaveSexton.DocProject.ExternalUI
{
	internal sealed class MSBuildLogger : Logger
	{
		#region Public Properties
		#endregion

		#region Private / Protected
		private readonly ProgramForm form;
		#endregion

		#region Constructors
		/// <summary>
		/// Constructs a new instance of the <see cref="MSBuildLogger" /> class.
		/// </summary>
		public MSBuildLogger(ProgramForm form)
		{
			if (form == null)
				throw new ArgumentNullException("form");

			this.form = form;
		}
		#endregion

		#region Methods
		public override void Initialize(Microsoft.Build.Framework.IEventSource eventSource)
		{
			if (eventSource == null)
				throw new ArgumentNullException("eventSource");

			eventSource.ProjectStarted += new Microsoft.Build.Framework.ProjectStartedEventHandler(eventSource_ProjectStarted);
			eventSource.ProjectFinished += new Microsoft.Build.Framework.ProjectFinishedEventHandler(eventSource_ProjectFinished);
			eventSource.ErrorRaised += new Microsoft.Build.Framework.BuildErrorEventHandler(eventSource_ErrorRaised);
			eventSource.MessageRaised += new Microsoft.Build.Framework.BuildMessageEventHandler(eventSource_MessageRaised);
			eventSource.StatusEventRaised += new Microsoft.Build.Framework.BuildStatusEventHandler(eventSource_StatusEventRaised);
			eventSource.WarningRaised += new Microsoft.Build.Framework.BuildWarningEventHandler(eventSource_WarningRaised);
		}
		#endregion

		#region Event Handlers
		private void eventSource_StatusEventRaised(object sender, Microsoft.Build.Framework.BuildStatusEventArgs e)
		{
			form.Status = e.Message;
		}

		private void eventSource_WarningRaised(object sender, Microsoft.Build.Framework.BuildWarningEventArgs e)
		{
			form.TraceBuildLine(e.Message);
		}

		private void eventSource_MessageRaised(object sender, Microsoft.Build.Framework.BuildMessageEventArgs e)
		{
			if (e.Importance != Microsoft.Build.Framework.MessageImportance.Low)
				form.TraceBuildLine(e.Message);
		}

		private void eventSource_ErrorRaised(object sender, Microsoft.Build.Framework.BuildErrorEventArgs e)
		{
			form.TraceBuildLine(e.Message);
		}

		private void eventSource_ProjectFinished(object sender, Microsoft.Build.Framework.ProjectFinishedEventArgs e)
		{
			form.TraceBuildLine(e.Message);
			form.TraceBuildLine();
		}

		private void eventSource_ProjectStarted(object sender, Microsoft.Build.Framework.ProjectStartedEventArgs e)
		{
			form.TraceBuildLine(e.Message);
		}
		#endregion
	}
}
